package com.qili.core.utils;

import com.qili.entity.base.BaseDict;
import com.qili.service.base.BaseDictService;
import com.qili.util.SpringUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import java.lang.ref.SoftReference;
import java.util.*;


/**
 * @Author Zhangshuo
 * @date 2020/11/4 14:23
 **/
@Component
public class DictUtil {
    //用于存储所有的字典数据
    private static Map<String, List<BaseDict>> _gmpDicts = new HashMap<String, List<BaseDict>>();
    private static SoftReference<Map<String,List<String>>> dictNames = new SoftReference<Map<String,List<String>>>(new HashMap<String,List<String>>());

    /**
     * @Author:Zhangshuo
     * @Description:加载所有的字典数据
     * @Date:15:08 2020/11/4
     */
    public static void loadAllDict() {
        System.out.println("---------------------------------------开始加载字典数据");
        ApplicationContext context = SpringUtil.getApplicationContext();
        BaseDictService baseDictService = context.getBean(BaseDictService.class);
        List<BaseDict> AlstAll = baseDictService.selectByTypeCode(null);
        _gmpDicts.clear();
        for (BaseDict dict : AlstAll) {
            String AsTypeCode = dict.getBadtTypecode();
            if (_gmpDicts.containsKey(AsTypeCode)) {
                List<BaseDict> AlstItem = _gmpDicts.get(AsTypeCode);
                AlstItem.add(dict);
            } else {
                List<BaseDict> AlstItem = new ArrayList<BaseDict>();
                AlstItem.add(dict);
                _gmpDicts.put(AsTypeCode, AlstItem);
            }
        }
        dictNames.clear();
        System.out.println("---------------------------------------字典数据加载完成");
    }

    /**
     * @param psTypeCode
     * @Author:Zhangshuo
     * @Description:根据字典编号获取字典列表
     * @Date:16:14 2020/11/4
     */
    public static List<BaseDict> getDictByType(DictTypes psTypeCode) {
        String AsTypeCode = psTypeCode.getType();
        if (_gmpDicts.containsKey(AsTypeCode)) {
            return _gmpDicts.get(AsTypeCode);
        } else {
            return null;
        }
    }

    public static List<BaseDict> getDictByTypeApp(String code) {
        String AsTypeCode = code;
        if (_gmpDicts.containsKey(AsTypeCode)) {
            return _gmpDicts.get(AsTypeCode);
        } else {
            return null;
        }
    }
    /**
    * @Author:zhaoyongke
    * @Description: 根据字典编号和类型获取字典值
    * @param psTypeCode
    * @param psDictCode
    * @Date:18:38 2020/11/30
    */
    public static BaseDict getDictByCode(DictTypes psTypeCode,String psDictCode) {
        String AsTypeCode = psTypeCode.getType();
        if (_gmpDicts.containsKey(AsTypeCode)) {
            List<BaseDict> Alist= _gmpDicts.get(AsTypeCode);
            for (int i = 0; i <Alist.size() ; i++) {
                BaseDict  baseDict= Alist.get(i);
                if(baseDict.getBadtCode().equals(psDictCode)){
                    return baseDict;
                }else{
                    continue;
                }
            }

        }
        return null;
    }



    /**
     * @param psTypeCode
     * @Author:Zhangshuo
     * @Description:根据字典编号获取字典列表
     * @Date:16:14 2020/11/4
     */
    public static List<BaseDict> getDictByType(DictTypes psTypeCode, String pskeyWord) {
        String AsTypeCode = psTypeCode.getType();
        if (_gmpDicts.containsKey(AsTypeCode)) {
            List<BaseDict> AlstRes=new ArrayList<BaseDict>();
            List<BaseDict> AlstTmp = _gmpDicts.get(AsTypeCode);
            for (BaseDict dict : AlstTmp) {
                String AsTmp=dict.getBadtName();
                if(AsTmp.contains(pskeyWord)){
                    AlstRes.add(dict);
                }
            }
            return AlstRes;
        } else {
            return null;
        }
    }

    public static boolean checkDic(String dictTypdCode,String value){
        boolean result = false;
        //如果字典中没有该字典类型，直接返回false;
        if(!_gmpDicts.containsKey(dictTypdCode)){
            return result;
        }
        //从缓存中获取所有字典类型  以及对应的所有字典名称：
        Map<String, List<String>> dictNamesMap = dictNames.get();
        if(dictNamesMap != null){
            //如果缓存中含有该字典类型，那么取出Map既是该字典类型的所有字典名称集合
            if(dictNamesMap.containsKey(dictTypdCode)){
                return (dictNamesMap.get(dictTypdCode) != null && dictNamesMap.get(dictTypdCode).contains(value));
            }else{
                // 那么去_gmpDicts对象中找出该字典类型对应的所有字典对象集合
                List<BaseDict> baseDicts = _gmpDicts.get(dictTypdCode);
                //stream转换为字典名称集合
                ArrayList<String> dictNameList = baseDicts.stream().collect(ArrayList<String>::new, (list1, val) -> list1.add(val.getBadtName()), (list1, list2) -> list1.addAll(list2));
                dictNamesMap.put(dictTypdCode,dictNameList);
                return (dictNameList!=null && dictNameList.size()>0 && dictNameList.contains(value));
            }
        }else{
            dictNames = new SoftReference<Map<String,List<String>>>(new HashMap<String,List<String>>());
            dictNamesMap = dictNames.get();
            // 那么去_gmpDicts对象中找出该字典类型对应的所有字典对象集合
            List<BaseDict> baseDicts = _gmpDicts.get(dictTypdCode);
            //stream转换为字典名称集合
            ArrayList<String> dictNameList = baseDicts.stream().collect(ArrayList<String>::new, (list1, val) -> list1.add(val.getBadtName()), (list1, list2) -> list1.addAll(list2));
            dictNamesMap.put(dictTypdCode,dictNameList);
            return (dictNameList!=null && dictNameList.size()>0 && dictNameList.contains(value));
        }
    }
}
